﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Text;
using System.IO;
using System.Web.UI.HtmlControls;
using System.Web.Security;

namespace Interfaces.Presupuesto
{
    public partial class Ver : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
                FormsAuthentication.RedirectToLoginPage();
         
            if (!IsPostBack)
            {
                if (Request.QueryString["gestion"] != null)
                {
                    try
                    {
                        string gestion = Request.QueryString["gestion"];
                        if (Processes.Presupuesto.GestorPresupuesto.existeGestion(gestion))
                        {
                            GridView1.DataSource = Processes.Presupuesto.GestorPresupuesto.getIE(gestion, 'I');
                            GridView1.DataSourceID = "";
                            GridView1.DataBind();
                            double montoIngresos, montoEgresos;
                            montoIngresos = Processes.Presupuesto.GestorPresupuesto.Total(gestion, 'I');
                            Ingresos.Text = montoIngresos.ToString();
                            GridView2.DataSource = Processes.Presupuesto.GestorPresupuesto.getIE(gestion, 'E');
                            GridView2.DataSourceID = "";
                            GridView2.DataBind();
                            montoEgresos = Processes.Presupuesto.GestorPresupuesto.Total(gestion, 'E');
                            Egresos.Text = montoEgresos.ToString();
                            double total = montoIngresos - montoEgresos;
                            Resultado.Text = (total).ToString();
                            if (total > 0)
                                Resultado.ForeColor = Color.Green;
                            if (total < 0)
                                Resultado.ForeColor = Color.Red;
                        }
                        else
                            MultiView1.ActiveViewIndex = 1;
                    }
                    catch (Exception)
                    {
                        MultiView1.ActiveViewIndex = 1;
                    }


                }
                else
                    MultiView1.ActiveViewIndex = 1;
            }
        }

        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           
        }
        private void ExportToExcel(string nameReport)
        {
            HttpResponse response = Response;
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            Page pageToRender = new Page();
            HtmlForm form = new HtmlForm();
            form.Controls.Add(GridView1);
            form.Controls.Add(Ingresos);
            form.Controls.Add(GridView2);
            form.Controls.Add(Egresos);
            
            pageToRender.Controls.Add(form);
            response.Clear();
            response.Buffer = true;
            response.ContentType = "application/vnd.ms-excel";
            response.AddHeader("Content-Disposition", "attachment;filename=" + nameReport);
            response.Charset = "UTF-8";
            response.ContentEncoding = Encoding.Default;
            pageToRender.RenderControl(htw);
            response.Write(sw.ToString());
            response.End();
        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            ExportToExcel("Presupuesto.xls");
        }
        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (Session["BuscarMaster"] != null)
            {
                string res = (string)Session["BuscarMaster"];
                this.MasterPageFile = res;
            }
        }
    }
}